package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import jdk.jfr.Threshold;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzyl.service.RoleService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleResourceMapper roleResourceMapper;

    @Override
    public PageResponse<RoleVo> findRoleVoPageResponse(RoleDto roleDto, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        Page<Role> page = roleMapper.findRoleVoPageResponse(roleDto);
        PageResponse<RoleVo> of = PageResponse.of(page, RoleVo.class);
        return of;
    }

    @Override
    public Boolean createRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.createRole(role);
        return true;
    }

    @Override
    public Set<String> findCheckedResources(Long roleId) {
        Set<String> set = roleResourceMapper.findCheckedResources(roleId);
        return set;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRole(List<String> roleIds) {
        roleMapper.deleteRole(roleIds);
        roleResourceMapper.deleteRolesource(roleIds);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.updateRole(role);
        // 判断是否修改对应角色的资源
        if (ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())) {
            return true;
        }

        Long id = role.getId();
        List<String> list = Arrays.asList(id.toString());
        roleResourceMapper.deleteRolesource(list);

        // 批量插入
        List<RoleResource> roleResources = new ArrayList<>();
        Arrays.asList(roleDto.getCheckedResourceNos()).forEach(roleResourceno -> {
            RoleResource build = RoleResource.builder()
                    .roleId(roleDto.getId())
                    .resourceNo(roleResourceno)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            roleResources.add(build);
        });

        if (EmptyUtil.isNullOrEmpty(roleResources)) {
            return true;
        }

        roleResourceMapper.insertBatch(roleResources);

        return true;
    }

    @Override
    public List<RoleVo> selectAll() {
        List<RoleVo> list = roleMapper.selectAll();

        return list;
    }


}
